<template>
	<view class="container">
		<view class="form-box">
			<view class="form">
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">收货人</view>
						 <input class="input" placeholder="请输入收货人" v-model="form.username" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">联系方式</view>
						 <input class="input" placeholder="请输入收货人联系方式" v-model="form.mobile" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">收货地址</view>
						 <input class="input" placeholder="请选择收货地址" v-model="form.address" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
			</view>
			<view class="btn-section">
				<button type="primary" size="default" @tap="save">保存</button>
			</view>
		</view>
	</view>
</template>

<script>
	import listCell from '@/components/list-cell/list-cell'
	import {addAddress} from '../../utils/http/index.js'
	import { mapState,mapActions } from 'vuex'
	export default {
		components: {
			listCell
		},
		data() {
			return {
				form: {
					username: '',
					isDefault: 1,
					mobile: '',
					address: '',
					id:''
				}
			}
		},
		computed:{
			...mapState(['member','addresses'])
		},
		async onLoad({id}) {
			if(id){
				this.id = id
				 let forms = this.$store.state.addresses.find(item => item.id == id)
				 this.form.username = forms.userName
				 this.form.mobile = forms.extend1
				 this.form.address = forms.address
				 this.form.isDefault = forms.isDefault
			}
			//为了方便演示，这里用本地缓存
			// if(id) {
			// 	this.form = this.$store.state.addresses.find(item => item.id == id)
			// }
		},
		methods: {
			...mapActions(['getlookAddress']),
			async save() {
				if(!this.form.username) return uni.showToast({title:'请输入收货人',icon:'none'})
				if(!this.form.mobile) return uni.showToast({title:'请输入联系方式',icon:'none'})
				if(!this.form.address) return uni.showToast({title:'请输入收货地址',icon:'none'})
				if(!this.id){
				}else{
					this.form.id = this.id
				}
				const {data} = await addAddress(this.form,this.member.token)
				if(data.code == 0){
					uni.showToast({
						title:data.msg,
						icon:'none'
					})
					this.getlookAddress(this.member.token)
					setTimeout(()=>{
						uni.navigateBack()
					},500)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.form-box {
		width: 100%;
		height: 100%;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		color: $text-color-base;
		
		.form-input {
			display: flex;
			align-items: center;
			width: 100%;
		}
		
		.label {
			width: 200rpx;
			font-size: $font-size-lg;
			color: $text-color-base;
			font-weight: 500;
		}
		
		.input {
			flex: 1;
			display: flex;
			align-items: center;
		}
		
		.radio-group {
			display: flex;
			justify-content: flex-start;
			
			.radio {
				padding: 10rpx 30rpx;
				border-radius: 6rpx;
				border: 2rpx solid $text-color-assist;
				color: $text-color-assist;
				font-size: $font-size-base;
				
				&.checked {
					background-color: $color-primary;
					color: $text-color-white;
					border: 2rpx solid $color-primary;
				}
			}
		}
		
		.btn-section {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			
			button {
				font-size: $font-size-base;
				height: 90rpx;
				border-radius: 50rem !important;
				width: 85%;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
